package com.epfresh.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.epfresh.R;
import com.epfresh.api.utils.LocalDisplay;
import com.epfresh.bean.NewHomeBean;

import multitype.ItemViewBinder;

/**
 * Author: Chen
 * Date: 2019/4/19
 * Desc:
 */
public class ViewBinderAd extends ItemViewBinder<NewHomeBean.HomeAdBean, ViewBinderAd.ViewHolder> {
    private OnAdClickListener onAdClickListener;
    private Context context;

    public ViewBinderAd(Context context) {
        this.context = context;
    }

    public void setOnAdClickListener(OnAdClickListener onAdClickListener) {
        this.onAdClickListener = onAdClickListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View itemView = inflater.inflate(R.layout.item_home_ad, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull final NewHomeBean.HomeAdBean item) {
        if (item.getImg() != null) {
            Glide.with(context).load(item.getImg())
//                .apply(RequestOptions.errorOf(R.drawable.pic_load_error))
                    .apply(RequestOptions.priorityOf(Priority.HIGH))
                    .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
                    .apply(new RequestOptions().transforms(new CenterCrop(),new RoundedCorners(LocalDisplay.dp2px(10))))
                    .transition(DrawableTransitionOptions.withCrossFade())
                    .into(holder.img_ad);
        }

        holder.img_ad.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onAdClickListener != null) {
                    onAdClickListener.onAdClick(item);
                }
            }
        });
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView img_ad;

        ViewHolder(View itemView) {
            super(itemView);

            img_ad = itemView.findViewById(R.id.img_ad);
        }

    }

    public interface OnAdClickListener {
        void onAdClick(NewHomeBean.HomeAdBean item);
    }

}
